﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Library;
using QLKH.PopUp.vattu;

namespace QLKH.View.Vattu
{
    public partial class dsLoaiDongHo : UserControl
    {
        DataView dt = new DataView();
        public dsLoaiDongHo()
        {
            InitializeComponent();
        }

        private void dsLoaiDongHo_Load(object sender, EventArgs e)
        {
            clLoaiDH clldh = new clLoaiDH();
            DataTable tdldh = clldh.danhsach();

            dataGrid.AutoGenerateColumns = false;
            dataGrid.DataSource = dt = tdldh.DefaultView;
        }

        public void Themloaidh(int id)
        {
            clLoaiDH clldh = new clLoaiDH();
            DataTable tdldh = clldh.danhsach();

            dataGrid.AutoGenerateColumns = false;
            dataGrid.DataSource = dt = tdldh.DefaultView;
        }

        public void Sualoaidh(int id)
        {
            clLoaiDH clldh = new clLoaiDH();
            DataTable tdldh = clldh.danhsach();

            dataGrid.AutoGenerateColumns = false;
            dataGrid.DataSource = dt = tdldh.DefaultView;
        }

        private void tool_add_Click(object sender, EventArgs e)
        {
            popThemLoaiDH usfritem = new popThemLoaiDH();

            usfritem.MyFunction = new popThemLoaiDH.ThemloaiDH(Themloaidh);
            usfritem.ShowDialog();
        }

        private void dataGrid_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            lbrowindex.Text = e.RowIndex.ToString();
        }

        private void tool_edit_Click(object sender, EventArgs e)
        {
            int row = int.Parse(lbrowindex.Text.Trim());
            if (row >= 0)
            {
                Int16 id = Int16.Parse(dataGrid.Rows[row].Cells["id"].Value.ToString());

                popSuaLoaiDH ufsin = new popSuaLoaiDH(id);
                ufsin.MyFunction = new popSuaLoaiDH.SuaLDH(Sualoaidh);
                ufsin.ShowDialog();
            }
            else {
                MessageBox.Show("Bạn chưa chọn loại đồng hồ");
            }
        }

        private void tool_delete_Click(object sender, EventArgs e)
        {
            int row = int.Parse(lbrowindex.Text.Trim());
            if (row >= 0)
            {
                Int16 id = Int16.Parse(dataGrid.Rows[row].Cells["id"].Value.ToString());
                clLoaiDH clldh = new clLoaiDH();

                int outid = clldh.delete_loaidh(id);
                if (outid >= 0)
                {
                    DataTable tdldh = clldh.danhsach();

                    dataGrid.AutoGenerateColumns = false;
                    dataGrid.DataSource = dt = tdldh.DefaultView;
                }
                else
                    MessageBox.Show("Bạn không thể xóa");
            }
            else
                MessageBox.Show("Bạn chưa chọn loại đồng hồ");
            }
        }
    }
